define('framework/pagination/GillionPaginationSizeDirectiveConstructor',['angular'], function (angular) {
    return function ($parse, $dataSourceManager,$timeout,$document) {

        var Pagination = function($scope, $element, $attrs){
            var me = this;
            me.scope = $scope;
            me.element = $element;
            me.attrs = $attrs;
        }

        Pagination.prototype.setPageSizeList = function(pageSizeList){
            if(angular.isArray(pageSizeList)){
                this.scope.pageSizeList = pageSizeList;
            }else if(pageSizeList){
                this.scope.pageSizeList = pageSizeList.split(',');
            }else{
                this.scope.pageSizeList = [5,10,20,30,50];
            }

        }

        Pagination.prototype.initDataSource = function(){
            var scope = this.scope;
            //$dataSourceManager.getDataSource(scope.sourceName).then(function(dataSource) {
            scope.$on(scope.sourceName,function(event,dataSource){
                scope.totalRecord = dataSource.totalRecord;
                scope.pageSize = dataSource.pageSize;
                scope.currentPage = dataSource.currentPage;

                scope.dataSource =  dataSource.records;
                scope.totalPage = dataSource.totalPage;

                scope.recordFrom = (dataSource.currentPage-1)*dataSource.pageSize+1;
                if(dataSource.records){
                    scope.recordTo = scope.recordFrom+dataSource.records.length-1;
                    if(scope.recordFrom+dataSource.records.length-1>scope.totalRecord){
                        scope.recordTo = scope.totalRecord;
                    }
                }else{
                    scope.recordTo = scope.recordFrom;
                }
                if(!scope.$root.$$phase){
                    scope.$digest();
                }
            });
        }

        Pagination.prototype.refresh = function(){
            var params,
                scope = this.scope;
            params = angular.extend({}, $dataSourceManager.dataSources[scope.sourceName].params  || {});
            $dataSourceManager.dataSources[scope.sourceName].params=params;
            this.initDataSource();
        }

        Pagination.prototype.goPage = function(pageNum){
            if(Number(pageNum)){
                var scope = this.scope;
                $dataSourceManager.dataSources[scope.sourceName].currentPage=pageNum;
                this.initDataSource();
            }
        }

            return {
            scope:{
                sourceName:'@'
            },
            replace:true,
            template:'<div class="pagesize pager-skin-tp">'+
                    '<span class="pager-desc"> 每页显示 </span>'+
                    '<div class="form-dorpdown">'+
                    '<input type="text" class="form-text" value="{{pageSize}}">'+
                    '<a type="button" class="btn dropdown-toggle"><i class="fi fi-caret" ng-click="showPageSize()"></i></a>'+
                    '<ul class="dropdown-menu"  ng-show="isShow" style="display:block">'+
                    '<li ng-repeat="ps in pageSizeList" ng-click="changePageSize(ps)"  class="fi"><a>{{ps}}</a></li>'+
                    '</ul></div></div>',
            restrict: 'E',
            compile:function(tElement, tAttrs){

                return function (scope, element,attrs) {
                    /*初始化参数*/
                    var keyIndex = -1,
                        timer;
                    scope.isShow =false;
                    scope.pagination = new Pagination(scope, element,attrs);
                    scope.showPageSize = function(){
                        scope.isShow = !scope.isShow;
                    }

                    scope.pagination.setPageSizeList(attrs.pageSizeList);
                    scope.pagination.initDataSource();
                    scope.changePageSize = function (pageSize){
                        $dataSourceManager.dataSources[scope.sourceName].pageSize = pageSize;
                        scope.pagination.initDataSource();
                        scope.isShow =false;
                    }

                    scope.pagination.initDataSource();

                    //点击隐藏
                    $document.on('click', function (event) {
                        if (element.find(event.srcElement || event.target).length === 0) {
                            scope.isShow =false;
                            scope.$digest();
                        }
                    });
                }
            }
        }
    }
});